package com.lph.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lph.model.BaseClass;
import com.lph.model.Menber;
import com.lph.model.Msg;
import com.lph.service.BaseServiceI;
import com.lph.service.MenberService;
import com.lph.service.ToLoginService;
import com.lph.util.EmailUtil;

@RequestMapping("/sc")
@Controller
public class SystemController {
	
	/*@Resource(name="baseService")//service的实现类
	private BaseServiceI<Msg> bsi;*/
	@Autowired
	private ToLoginService tls;
	
	@Autowired
	private MenberService ms;
	
	@RequestMapping("/index.coc")
	public String index(){
		return "/index";
	}

	@RequestMapping("/left.coc")
	public String left(){
		return "/left";
	}

	@RequestMapping("/center.coc")
	public String center(){
		return "/center";
	}
	@RequestMapping("/top.coc")
	public String top(){
		return "/top";
	}
	@RequestMapping("/main.coc")
	public String main(){
		return "/main";
	}
	@RequestMapping("/toLogin.coc")
	@ResponseBody
	public Msg toLogin(Menber m,HttpSession session){
		
		return tls.toLogin(m,session);
		
	}
	
	@RequestMapping("/loginOut.coc")
	@ResponseBody
	public String loginOut(HttpSession session){
		session.removeAttribute("loginName");
		return "success";
		
	}

	@RequestMapping("/forgetPwd.coc")
	public String forgetPwd(){
		return "/forgetPwd";
		
	}
	
	
	@RequestMapping("/sendEmail.coc")
	@ResponseBody
	public Msg sendEmail(HttpServletRequest req){
		String loginName=req.getParameter("loginName");
		
		return ms.sendEmail(loginName, req);
		
	}
	
	@RequestMapping("/editPwd.coc")
	public void editPwd(HttpServletRequest req,HttpServletResponse res) throws  Exception{
		String loginName=req.getParameter("login");
		String ms=req.getParameter("ms");
		String token=req.getParameter("token");
		
		Msg m= this.ms.editPwd(req,loginName,ms,token );
		
		//设置编码格式
		res.setContentType("text/html;charset=utf-8");
		
		if(!m.isIs()){
	
		PrintWriter out=res.getWriter();
		out.print("<script language='javascript'>");
		out.print("alert('"+m.getMsg()+"')");
		out.print("</script>");
		}else{
			//页面查询
			req.setAttribute("m", m.getObject());
			req.getRequestDispatcher("/sc/pwdPage.coc").forward(req, res);
		}
	}
	@RequestMapping("/pwdPage.coc")
	public String pwdPage(){
		return "/editPwd";
		
	}
	
	@RequestMapping("/getMobileCode.coc")
	@ResponseBody
	public Msg getMobileCode(HttpServletRequest req){
		String mobile=req.getParameter("mobile");
		Msg m=new Msg("验证码发送失败", false);
		if(mobile!=null){
			boolean flat=EmailUtil.sendSms(mobile, req);
			if(flat){
				m=new Msg("验证码发送成功,有效5分钟",true);
			}
		}
		return m;
	}
}
